package com.ruoyi.web.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.shop.domain.OpenIdMemberCfgBean;
import com.ruoyi.shop.mapper.OpenIdMemberCfgBeanMapper;
import com.ruoyi.web.domain.Member;
import com.ruoyi.web.mapper.MemberMapper;
import com.ruoyi.web.service.IMemberService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 会员Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-11-07
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class MemberServiceImpl implements IMemberService
{
    private final MemberMapper memberMapper;

    private final OpenIdMemberCfgBeanMapper openIdMemberCfgBeanMapper;

    private final String WECHAT = "2";
    private final String SYSTEM = "1";

    /**
     * 查询会员
     * 
     * @param ID 会员主键
     * @return 会员
     */
    @Override
    public Member selectMemberByID(Long ID)
    {
        return memberMapper.selectMemberByID(ID);
    }

    /**
     * 查询会员列表
     * 
     * @param member 会员
     * @return 会员
     */
    @Override
    public List<Member> selectMemberList(Member member)
    {
        List<Member> members = memberMapper.selectMemberList(member);
        //优化客户渠道
        for(Member member1 : members){
            String channel = member1.getCHANNEL();
            member1.setCHANNEL("1".equals(channel)?"管理员添加":"微信");
        }
        return members;
    }

    /**
     * 新增会员
     * 
     * @param member 会员
     * @return 结果
     */
    @Override
    public int insertMember(Member member)
    {
        member.setCreateTime(DateUtils.getNowDate());
        updateCfg(member,"add");
        return memberMapper.insertMember(member);
    }

    /**
     * 修改会员
     * 
     * @param member 会员
     * @return 结果
     */
    @Override
    public int updateMember(Member member)
    {
        member.setUpdateTime(DateUtils.getNowDate());
        updateCfg(member,"update");
        return memberMapper.updateMember(member);
    }

    /**
     * 批量删除会员
     * 
     * @param IDs 需要删除的会员主键
     * @return 结果
     */
    @Override
    public int deleteMemberByIDs(String IDs)
    {
        return memberMapper.deleteMemberByIDs(Convert.toStrArray(IDs));
    }

    /**
     * 删除会员信息
     * 
     * @param ID 会员主键
     * @return 结果
     */
    @Override
    public int deleteMemberByID(Long ID)
    {
        return memberMapper.deleteMemberByID(ID);
    }

    private void updateCfg(Member member, String optType){

        String channel = member.getCHANNEL();
        //微信渠道，添加关联表
        if(WECHAT.equals(channel)){
            String openId = member.getOpenId();
            OpenIdMemberCfgBean openIdMemberCfgBean = new OpenIdMemberCfgBean();
            openIdMemberCfgBean.setOpenId(openId);
            if("add".equals(optType)){
                Long memberId = memberMapper.getId();
                if(memberId == null){
                    memberId = 1L;
                }
                member.setID(memberId);
                openIdMemberCfgBean.setMemberId(memberId);
                Long cfgId = openIdMemberCfgBeanMapper.getId();
                if(cfgId == null){
                    cfgId = 1L;
                }
                openIdMemberCfgBean.setId(cfgId);
                openIdMemberCfgBeanMapper.insertOpenIdMemberCfg(openIdMemberCfgBean);
            }else {
                Long memberId = member.getID();
                int cfgCount = openIdMemberCfgBeanMapper.getCountByMember(memberId);
                if(cfgCount != 0 ){
                    openIdMemberCfgBeanMapper.updateOpenIdMemberCfg(openIdMemberCfgBean);
                }else {
                    openIdMemberCfgBean.setMemberId(memberId);
                    Long cfgId = openIdMemberCfgBeanMapper.getId();
                    if(cfgId == null){
                        cfgId = 1L;
                    }
                    openIdMemberCfgBean.setId(cfgId);
                    openIdMemberCfgBeanMapper.insertOpenIdMemberCfg(openIdMemberCfgBean);
                }
            }

        }
    }
}
